package zzps.exeed.kernel.aop;

import zzps.exeed.kernel.holder.MethodHolder;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.List;

public class InvocationImpl implements Invocation{
    private  Object target;
    private MethodHolder methodHolder;
    private Object[] args;
    private List<Aspect> aspectList;
    private int position = 0;

    public InvocationImpl(Object target, MethodHolder methodHolder, Object[] args) {
        this.target = target;
        this.methodHolder = methodHolder;
        this.args = args;
        aspectList = methodHolder.getAspectList();
    }

    @Override
    public Object target() {
        return target;
    }

    @Override
    public Object[] args() {
        return args;
    }

    @Override
    public MethodHolder method() {
        return methodHolder;
    }

    public Object invoke() throws Throwable{
        int index = position++;
        if (index >= aspectList.size()){
            return methodHolder.invoke(target,args);
        }else {
            return aspectList.get(index).advise(this);
        }
    }
}
